from utils.config import Config, REPORT_PATH
from appium import webdriver
import time as t
import os
from selenium.webdriver.support.ui import WebDriverWait


DESIRED_CAPS = Config().get('desired_caps')
# DESIDOJD =Config().get('desired_capss')
# PACKAGE = Config().get('desired_caps')['appPackage']
SERVER = Config().get('setup').get('server').lower()


class AppPackage:  # 测试包对象，集成测试基本方法，例如截图等

    def __init__(self):
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', DESIRED_CAPS)
        # self.driver = webdriver.Remote('http://localhost:4724/wd/hub', DESIDOJD)
        WebDriverWait(self.driver, timeout=15, poll_frequency=0.5, ignored_exceptions=None)

    def save_screen_shot(self, name='screen_shot'):  # 截图方法
        day = t.strftime('%Y%m%d', t.localtime(t.time()))
        screen_shot_path = os.path.join(REPORT_PATH, 'screen_shot_%s' % day)  # 创建日期文件夹
        if not os.path.exists(screen_shot_path):
            os.makedirs(screen_shot_path)
        tm = t.strftime('%H%M%S', t.localtime(t.time()))
        screen_shot = self.driver.get_screenshot_as_file(os.path.join(screen_shot_path, '%s_%s.png' % (name, tm)))
        return screen_shot  # 生成文件名为时间的截图

    def quit(self):
        self.driver.quit()


if __name__ == '__main__':
    driver = webdriver.Remote('http://localhost:4723/wd/hub', DESIRED_CAPS)
